﻿using System;
using System.Collections.Generic;
using UnityEngine;
using View.Instant;
using ZWFrameWork;
using AnimationState = UnityEngine.AnimationState;

public class Tool
{
    /// <summary>
    /// GetMaxLenString(过长字符串显示处理)
    /// </summary>
    /// <param name="sourcesString"></param>
    /// <param name="len"></param>
    /// <returns></returns>
    public static string GetMaxLenString(string sourcesString,int maxLen)
    {
        int sourceLen = sourcesString.Length;
        string dstString = sourcesString;
        
        if (sourceLen > maxLen)
        {
            dstString = dstString.Substring(maxLen);
            dstString = dstString + "...";
        }

        return dstString;
    }
    
    /// <summary>
    /// 序列化一个string返回回去，协议给过来的带 \0 这里主要处理这个，顺带处理一些换行什么的
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string FormatString(string str)
    {
        string strshuzhi = str;
        strshuzhi = strshuzhi.Replace("\n", "");
        strshuzhi = strshuzhi.Replace("\a", "");
        strshuzhi = strshuzhi.Replace("\r", "");
        strshuzhi = strshuzhi.Replace("\0", "");
        return strshuzhi;
    }

    /// 给一个数值，如果有小数返回几位小数，如果没有小数，返回整数   DevcimalToString
    /// </summary>
    /// <param name="num"></param>
    /// <param name="scale"></param>
    /// <returns></returns>
    public static double GetDecimalSplitNum(double num, int scale = 2)
    {
        string numtostring = num.ToString();
        
        int index = numtostring.IndexOf(".");
        int length = numtostring.Length;

        if (index != -1)
        {
            string dm = string.Format("{0}.{1}", numtostring.Substring(0, index), numtostring.Substring(index + 1, Mathf.Min(length - index - 1, scale)));
            // return System.Convert.ToDecimal(dm);
            return System.Convert.ToDouble(dm);
        }
        else
        {
            string dm = num.ToString();
            // return System.Convert.ToDecimal(dm);
            return System.Convert.ToDouble(dm);
        }
    }

    /// <summary>
    /// 获取毫秒时间戳
    /// </summary>
    /// <returns></returns>
    public static long OnGetCurrentTimerForMillisecond()
    {
        TimeSpan st1 = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
        return Convert.ToInt64(st1.TotalMilliseconds);

        // TimeSpan st2 = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
        // return  Convert.ToInt64(st2.TotalMilliseconds);
    }
    /// <summary>
    /// 获取当前时间戳
    /// </summary>
    /// <returns></returns>
    public static long OnGetCurrentTimer()
    {
        return DateTime.UtcNow.Ticks;
        // return DateTime.Now.ToUniversalTime().Ticks;
    }
    
    /// <summary>
    /// 获取一个指定时间戳
    /// </summary>
    /// <returns></returns>
    public static DateTime OnGetDateTimeByLongTimer(long longtime)
    {
        var daime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0,0,0,DateTimeKind.Utc),TimeZoneInfo.Local);
        return daime.AddMilliseconds(longtime);
    }

    /// <summary>
    /// 获取当前时间戳 秒
    /// </summary>
    /// <returns></returns>
    public static long OnGetCurDateTimeByMin()
    {
        return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    }

    public static string Encode_GameId(uint id)
    {
        string shuzu = id.ToString();
        string jieshu = "";
        int index = 0;
        for (int i = 0; i < shuzu.Length; i++)
        {
            int shuzi = shuzu[i];

            jieshu += shuzi;
        }

        return jieshu;
    }
    public static uint Decode_GameId(string id)
    {
        string zhanghao = id;
        List<string> list = new List<string>();
        for (int i = 0; i < zhanghao.Length*0.5; i++)
        {
            string sjuju = zhanghao.Substring(i * 2, 2);
            list.Add(sjuju);
        }

        string shuju = "";
        Debug.LogError("推广员数据1："+id);
        for (int i = 0; i < list.Count; i++)
        {
            shuju += ((char)int.Parse(list[i])).ToString();
        }
        Debug.LogError("推广员数据2："+shuju);
        return uint.Parse(shuju);
    }
}